local sk__jianyue = fk.CreateSkill {

  name = "sk__jianyue",

  tags = {},

}



sk__jianyue:addEffect(fk.EventPhaseStart, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    local isHandcardValid = table.every(player.room.alive_players, function(p)
      return p:getHandcardNum() >= target:getHandcardNum()
    end)
    return player:hasSkill(sk__jianyue.name) and target.phase == Player.Finish and not target.dead and isHandcardValid
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, sk__jianyue.name, nil, "#sk__jianyue-invoke::" .. target.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, { target.id })
    local isHandcardValidWhile = table.every(player.room.alive_players, function(p)
      return p:getHandcardNum() >= target:getHandcardNum()
    end)
    while not target.dead and #room.discard_pile > 0 and isHandcardValidWhile do
      local randomCard = table.random(room.discard_pile)
      room:moveCardTo(randomCard, Card.PlayerHand, target, fk.ReasonJustMove, sk__jianyue.name, nil, true, target)
      isHandcardValidWhile = table.every(player.room.alive_players, function(p)
        return p:getHandcardNum() >= target:getHandcardNum()
      end)
    end
  end,
})

return sk__jianyue
